﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using syc.Helper;
using System.Drawing;

namespace syc.ZhiJian.Report
{
    public partial class 入库明细 : System.Web.UI.Page
    {
        protected string year;
        protected string month;
        protected string RiQiStart;
        protected string RiQiEnd;
        protected string RiQi;
        protected DateTime start, end;
        private string sql = @"
SELECT dbo.原料供货单位.名称, 
	   kc.入库日期, dbo.产品.规格, 
	   SUM(kc.支数) AS 支数, 
	   SUM(kc.重量) AS 重量, 
	   SUM(kc.长度) AS 长度,
	   (SELECT distinct kc2.炉号+'' 
		FROM dbo.产品库存 as kc2 
	    WHERE (kc2.入库日期=kc.入库日期) AND
	          (kc2.产品类别ID = *产品*)  AND
              (kc2.生产单位ID = *分厂*)
	    FOR XML PATH('')
	   ) AS 炉号,
	   (SELECT distinct RTRIM(kc3.捆号)+'为'+CAST([支数] AS varchar(4))+'支 '
		FROM dbo.产品库存 as kc3
	    WHERE (kc3.入库日期=kc.入库日期) AND
			  (kc3.支数!=*标准支数*	) AND
			  (kc3.产品类别ID = *产品*) AND
              (kc3.生产单位ID = *分厂*)
	    FOR XML PATH('')	    
	   ) AS 备注
FROM dbo.产品库存 AS kc INNER JOIN
     dbo.产品 ON kc.产品类别ID = dbo.产品.ID INNER JOIN
     dbo.原料供货单位 ON kc.原料供货ID = dbo.原料供货单位.ID
WHERE (kc.产品类别ID = *产品*) AND
      ((kc.入库日期) between '*起始日期*' and '*截止日期*') AND
      (kc.生产单位ID=*分厂*) AND
      (kc.状态!=0)
GROUP BY dbo.原料供货单位.名称, kc.入库日期, dbo.产品.规格";
        protected static DateTime date;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";

            if (!this.IsPostBack)
            {
                bool success;
                success = DateTime.TryParse(Date1.Text, out start);
                success = DateTime.TryParse(Date2.Text, out end);
                shengchanxian.Visible = false;
                if (success)
                {
                    RiQiStart = start.ToString("yyyy-MM-dd");
                    RiQiEnd = end.ToString("yyyy-MM-dd");
                }
                else
                {
                    RiQiStart = DateTime.Now.ToString("yyyy-MM-dd");
                    RiQiEnd = DateTime.Now.ToString("yyyy-MM-dd");

                }

                sql = sql.Replace("*起始日期*", RiQiStart);
                sql = sql.Replace("*截止日期*", RiQiEnd);
                sql = sql.Replace("*分厂*", DList_Fenchang.SelectedValue);
                sql = sql.Replace("*产品*", DList_Produce.SelectedValue);
                if (DList_Produce.SelectedValue == "1")
                {
                    sql = sql.Replace("*标准支数*", "19");
                }
                else
                {
                    sql = sql.Replace("*标准支数*", "7");
                }
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }

            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];

            //这一段分组的代码逻辑会强制执行，所以无需再在各个事件中重写
            GridViewHelper helper = new GridViewHelper(this.GridView1);
            helper.RegisterGroup("名称", true, true);
            helper.RegisterSummary("支数", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("重量", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("长度", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("支数", SummaryOperation.Sum);
            helper.RegisterSummary("重量", SummaryOperation.Sum);
            helper.RegisterSummary("长度", SummaryOperation.Sum);
            helper.GroupHeader += new GroupEvent(helper_GroupHeader);
            helper.GroupSummary += new GroupEvent(helper_GroupSummary);
            helper.GeneralSummary += new FooterEvent(helper_GeneralSummary);
            helper.ApplyGroupSort();
            Date1.TextChanged += new EventHandler(DList_month_SelectedIndexChanged);
            Date2.TextChanged += new EventHandler(DList_month_SelectedIndexChanged);
        }

        //自定义分组标题样式
        private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            row.Cells[0].Height = 33;
            row.BackColor = Color.LightGray;
            row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;

        }

        //自定义合计标题样式
        private void helper_GeneralSummary(GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.BackColor = Color.LightSlateGray;
            row.Cells[0].Text = "总计:";
            row.Height = 40;
        }


        //自定义合计标题样式
        private void helper_GroupSummary(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.Cells[0].Text = "小计:";
            row.Height = 40;
        }

        //各个下拉列表变化时都调用了它
        protected void DList_month_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool success;
            success = DateTime.TryParse(Date1.Text, out start);
            success = DateTime.TryParse(Date2.Text, out end);
            if (success)
            {
                RiQiStart = start.ToString("yyyy-MM-dd");
                RiQiEnd = end.ToString("yyyy-MM-dd");
            }
            else
            {
                RiQiStart = DateTime.Now.ToString("yyyy-MM-dd");
                RiQiEnd = DateTime.Now.ToString("yyyy-MM-dd");

            }

            sql = sql.Replace("*起始日期*", RiQiStart);
            sql = sql.Replace("*截止日期*", RiQiEnd);
            sql = sql.Replace("*分厂*", DList_Fenchang.SelectedValue);
            sql = sql.Replace("*产品*", DList_Produce.SelectedValue);
            if (DList_Produce.SelectedValue == "1")
            {
                sql = sql.Replace("*标准支数*", "19");
            }
            else
            {
                sql = sql.Replace("*标准支数*", "7");
            }
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }


        //导出按钮
        protected void Button1_Click(object sender, EventArgs e)
        {

            GridView1.DataBind();
            GridViewHelper helper = new GridViewHelper(this.GridView1);
            helper.RegisterGroup("名称", true, true);
            helper.RegisterSummary("支数", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("重量", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("长度", SummaryOperation.Sum, "名称");
            helper.RegisterSummary("支数", SummaryOperation.Sum);
            helper.RegisterSummary("重量", SummaryOperation.Sum);
            helper.RegisterSummary("长度", SummaryOperation.Sum);
            helper.GroupHeader += new GroupEvent(helper_GroupHeader);
            helper.GroupSummary += new GroupEvent(helper_GroupSummary);
            helper.GeneralSummary += new FooterEvent(helper_GeneralSummary);
            helper.ApplyGroupSort();
            //var ss = GridViewExportHelper.GetExportGridContent(GridView1, false,false);
            //GridViewExportHelper.WriteResponse(ss, "application/vnd.ms-excel", true, "Filename.xls");   


            Response.Clear();

            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "utf-8";

            // If you want the option to open the Excel file without saving than
            // comment out the line below
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Response.ContentType = "application/vnd.xls";
            var title = Date1.Text + "到" + Date2.Text + DList_Fenchang.SelectedItem.Text + "分厂" + DList_Produce.SelectedItem.Text + "入库明细";
            HttpContext.Current.Response.Write(
    @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html>
<head>
<meta http-equiv=""Content-Language"" content=""zh-cn""/>
<meta http-equiv=""Content-Type"" content="""
    + HttpContext.Current.Response.ContentType + "; charset=utf-8\"/></head><body><h2><center>" + title + "</center></h2>");
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.Write("</body></html>");
            Response.End();

        }

        public override void VerifyRenderingInServerForm(Control control)
        {

            // Confirms that an HtmlForm control is rendered for the
            //specified ASP.NET server control at run time.

        }
        //生产线变化
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        //单独处理一下5分厂1、2号线的问题
        protected void DList_Fenchang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DList_Fenchang.SelectedValue == "5")
            {
                shengchanxian.Visible = true;
            }
            else {
                shengchanxian.Visible = false;
            }
        }
    }
}
